/**
 * 商家列表模块
 */
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	$(function(){
		//商家列表登录框
		$(document).on("click",".ldh-userBox>p",function(){		
			if($(this).parent().hasClass("on")){$(this).parent().removeClass("on");}
			else{$(this).parent().addClass("on");}	
			$(document).on("click",function(event){
				var _target=$(event.target).parents();
				if(!_target.hasClass("ldh-userBox")){
					  $(".ldh-userBox").removeClass("on");				  
				}	
			});			
		});
		//商家列表分类选中状态
		$(document).on("click",".ldh-storeList-class span",function(){
			$(this).addClass("on").siblings().removeClass("on");
		});
		//商家列表条件筛选（隐藏方法$(".btn-group").removeClass("open");）
		$(document).on("click",".btn-group .btn",function(){
			if(!$(this).parent().hasClass("open")){$(this).parent().addClass("open");}else{$(this).parent().removeClass("open")}
			$(document).on("click",function(event){
				var _target=$(event.target).parents();
				if(!_target.hasClass("btn-group")){
					  $(".btn-group").removeClass("open");				  
				}else{_target.siblings(".btn-group").removeClass("open");}	
			});
		});
		function btnGroup(){ $(".btn-group").removeClass("open")}
		
	});
	

	
	var pageSize=10;
	
	var base = require("../../custom/base/component-base.js");
	
	base.init(base);
	

	
	var validate = require("../../custom/base/component-validate.js");
	
	var async = require("../../custom/base/component-async.js");

	require("../../custom/base/component-core.js");
	
	require("../../custom/base/component-map.js");

	require("../../custom/base/component-page.js");

	
	//初始化函数
	exports.init=function(m){
	
		window.$ldh_shop_list=m;
		
		base.setFromPage("shopList");
		
		var userId=$("#session_userid").val();
		
		if(!validate.isNull(userId)){
			
			base.hasLoginShow(userId,$("#session_username").val(),$("#session_iconurl").val());
		}
		
		base.initMap();
		
		base.inputAdress();
		
		if(validate.isNull(base.getCookie("cityName"))){
			
			base.setCookie("cityId","440100");
			
			base.setCookie("cityName","广州市");
			
		}
		
		$("#cityName").html(base.getCookie("cityName"));
		
		if(validate.isNull(base.getCookie("address")) || validate.isNull(base.getCookie("lng")) || validate.isNull(base.getCookie("lat"))){
			
			base.myLocation();
		}else{
			
			$("#address").html(base.subString(base.getCookie("address"),10));
			
			$("#address").attr("title",base.getCookie("address"));
			
			base.myLocationHide();
			
			
		}
		
		var url=decodeURI(document.URL);
		
		if(url.indexOf("?")>0){
			
			var params=url.split("?")[1];
			
			params=base.decodeParams(params);
			
			if(params.indexOf("shopTypeId")>=0){
								
				var parentTypeId=params.split("=")[1];
				
				
				exports.setShopType(parentTypeId);
								
				$("#div_search_shop").show();
				
			}else if(params.indexOf("keyword")>=0){
								
				var keyword=params.split("=")[1];
				
				base.initSearch();
				
				$("#inputSearch").val(keyword);
				
				base.switchSearchType("2","店铺","搜索地点附近的店铺");
				
				$("#shopTypeList").hide();
				
				$("#div_search_shop").hide();
				
				$("#div_search").show();
				
				$("#div_search").css("float","left");
				
				exports.setShopName(keyword);
				
				exports.getShopList();
				
				
				
			}
			
			
		}
		
		exports.getShopTypeByParentId();

		
		base.getSearchHistoryCookie();
		
		document.title="云便利商店 附近商家列表";
		
	};
	
	//设置列表页码
	exports.setCurrentPage=function(currentPage){
		
		this.currentPage=currentPage;
		
	};
	
	//获取列表页码
	exports.getCurrentPage=function(){
		
		return this.currentPage==null?1:this.currentPage;
		
	};
	

	//设置排序帅选
	exports.setSort=function(sort,sortName){
		
		this.sort=sort;
		
		$("#sortName").html(sortName);
		
		$(".btn-group").removeClass("open");	
		
		exports.setCurrentPage(1);
		
		exports.getShopList();
	};
	
	//获取排序帅选
	exports.getSort=function(){
		
		return this.sort==null?"2":this.sort;
		
	};
	
	//设置店铺状态帅选
	exports.setBusinessStatus=function(businessStatus,businessStatusName){
		
		this.businessStatus=businessStatus;
		
		$("#businessStatusName").html(businessStatusName);
		
		$(".btn-group").removeClass("open");
		
		exports.setCurrentPage(1);
		
		exports.getShopList();
		
	};
	
	//获取店铺状态帅选
	exports.getBusinessStatus=function(){
		
		return this.businessStatus==null?"":this.businessStatus;
		
	};
	
	//设置商家类型帅选
	exports.setShopType=function(shopTypeId){
		
		this.shopTypeId=shopTypeId;
		
		exports.setCurrentPage(1);
		
		exports.getShopList();
	};
	
	//获取商家类型帅选
	exports.getShopType=function(){
		
		return this.shopTypeId==null?"":this.shopTypeId;
		
	};
	
	//设置搜索商家名称
	exports.setShopName=function(shopName){
		
		this.shopName=shopName;
		
	};
	
	//获取搜索商家名称
	exports.getShopName=function(){
		
		return this.shopName==null?"":this.shopName;
		
	};
	
	//查询商家分类
	exports.getShopTypeByParentId=function(){

		var typeId=exports.getShopType();
		
		if(typeId=="1"){
			
			$("#icon_store_type").html('<p><img src="'+ctx+'/v1/img/food_1.png"/></p><p><img src="'+ctx+'/v1/img/food_2.png"/></p>');
			
		}else if(typeId=="2"){
			
			$("#icon_store_type").html('<p><img src="'+ctx+'/v1/img/goods_1.png"/></p><p><img src="'+ctx+'/v1/img/goods_2.png"/></p>');
		}else if(typeId=="3"){
			
			$("#icon_store_type").html('<p><img src="'+ctx+'/v1/img/services_1.png"/></p><p><img src="'+ctx+'/v1/img/services_2.png"/></p>');
		}else if(typeId=="4"){
			
			$("#icon_store_type").html('<p><img src="'+ctx+'/v1/img/feature_1.png"/></p><p><img src="'+ctx+'/v1/img/feature_2.png"/></p>');
		}else if(typeId=="5"){
			
			$("#icon_store_type").html('<p><img src="'+ctx+'/v1/img/express_1.png"/></p><p><img src="'+ctx+'/v1/img/express_2.png"/></p>');
		}else if(typeId=="50"){
			
			$("#icon_store_type").html('<p><img src="'+ctx+'/v1/img/fresh_1.png"/></p><p><img src="'+ctx+'/v1/img/fresh_2.png"/></p>');
		}
				
		var params = {
				parentId :typeId
		};
		
			eval(
					Wind.compile("async", function() {

						var data = $await(async.DDF_AsyncRequest("POST", ctx + "/shopweb/ShopsType/showShopsTypes.do", params));
						
						
						if(data["code"]==1){
							
						//	{"shopTypeList":[{"mcc":0,"memo":"","parentId":"","shopTypeId":"1","shopTypeName":"全部","sort":0},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"6","shopTypeName":"中餐","sort":6},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"7","shopTypeName":"西餐","sort":7},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"8","shopTypeName":"日韩","sort":8},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"9","shopTypeName":"蛋糕面包","sort":9},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"10","shopTypeName":"甜品","sort":10},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"12","shopTypeName":"快餐","sort":12},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"13","shopTypeName":"其它餐饮","sort":13},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"49","shopTypeName":"肉菜","sort":49}],"code":"1","message":"调用成功"}
							var htmlStr="";
							
							for (var i = 0; i < data["shopTypeList"].length; i++) {
								if(i==0){
									htmlStr += ' <span class="on" onclick=$ldh_shop_list.setShopType("'+data["shopTypeList"][i]["shopTypeId"]+'")>'+data["shopTypeList"][i]["shopTypeName"]+'</span>';
								}else{
									htmlStr += ' <span onclick=$ldh_shop_list.setShopType("'+data["shopTypeList"][i]["shopTypeId"]+'")>'+data["shopTypeList"][i]["shopTypeName"]+'</span>';
								}
							}
							
							$("#shopTypeList").html(htmlStr);

						}
						
						
					}))().start();
		
		
	};
	
	//搜索商家分类列表中的商家
	exports.searchShop=function(){
		
		var keyword=$("#search_shop_name").val();
		
		exports.setCurrentPage(1);
		
		exports.setShopName(keyword);
		
		exports.getShopList();
		
		
	};
	
	//查询商家列表
	exports.getShopList=function(){
		
		
		var lng=base.getCookie("lng");
		
		var lat=base.getCookie("lat");
				
		var params = {
				currentPage :exports.getCurrentPage(),
				pageSize :pageSize,
				lng :lng,
				lat :lat,
				shopName:exports.getShopName(),
				typeId :exports.getShopType(),
				businessStatus :exports.getBusinessStatus(),
				sort :exports.getSort()
		};
		
		base.showLoading("#shopList","",true);
		
			eval(
					Wind.compile("async", function() {

						var data = $await(async.DDF_AsyncRequest("POST", ctx + "/shopweb/Shop/shopList.do", params));
						
						
						if(data["code"]==1){
		
							var htmlStr="";
							
						/*	if(data["rflag"]==1){
								
								htmlStr='<li>找不到符合条件的商家.下面为你推荐优质商家.你也可以马上"<font color="#ed5d3a">开店进驻</font>"哦.</li>';
							}*/

							for (var i = 0; i < data["shopList"].length; i++) {
							
							var distance=data["shopList"][i]["distance"];
							
							var introduce=data["shopList"][i]["introduce"];
							
							var minAmount = data["shopList"][i]["minAmount"];
							
							var businessStatus = data["shopList"][i]["businessStatus"];

							var score=(data["shopList"][i]["score"]/5)*100;
							
							var businessHours=data["shopList"][i]["businessHours"];
							
							var range= data["shopList"][i]["range"];
							
							if(validate.isNull(introduce))
								introduce="暂无";
							
							if(validate.isNull(businessHours))
								businessHours="全天";
							
							if(distance.indexOf(">")>=0){
								
								distance=distance+"km";
								
							}else if(distance<1){
								
								distance=(distance*1000)+"m";
								
							}else{
								
								distance="约"+parseInt(distance)+"km";
							}
							
							if(minAmount=="" || minAmount=="0"){
								minAmount="不限";
							}else{
								minAmount=minAmount+"元";
							}

							if(range=="" || range=="0"){
								range="不限";
							}else{
								range=range+"米";
							}
										htmlStr += '<li style="cursor:default">';
										
										if(data["shopList"][i]["logoUrl"]!="" && data["shopList"][i]["logoUrl"].indexOf(".")>0){
											htmlStr += '<div style="cursor:pointer" class="ldh-storeList-pic"  onclick=$ldh_base.jumpToShopIndex("'+data["shopList"][i]["shopId"]+'")><img src="'+fileOpenUrl+data["shopList"][i]["logoUrl"]+'"></div>';
										}else{
											htmlStr += '<div style="cursor:pointer" class="ldh-storeList-pic"  onclick=$ldh_base.jumpToShopIndex("'+data["shopList"][i]["shopId"]+'")><img src="'+ctx+'/v1/img/default_234_220.png"></div>';

										}
										
										htmlStr += '<div class="ldh-storeList-info">';
										htmlStr += '<div class="ldh-storeList-infoT">';
										htmlStr += '<div class="fl-left">';
										htmlStr += '<h3 style="cursor:pointer" class="fl-left"  onclick=$ldh_base.jumpToShopIndex("'+data["shopList"][i]["shopId"]+'")>'+data["shopList"][i]["shopName"]+'</h3>';
										htmlStr += '<p class="fl-left">'+base.getSpeHtml(data["shopList"][i]["speId"])+'</p>';
										htmlStr += '<div class="ldh-storeList-hideBox">';
										htmlStr += '<dl>';
										htmlStr += '<dt><h3>'+data["shopList"][i]["shopName"]+'</h3></dt>';
										htmlStr += base.getSpeListHtml(data["shopList"][i]["speId"]);
										htmlStr += '</dl>';
										htmlStr += '<div>';
										htmlStr += '<p>商家公告</p>';
										htmlStr += '<span>'+introduce+'</span>';
										htmlStr += '</div>';
										htmlStr += '</div>';
										htmlStr += '</div>';
										htmlStr += '<div class="fl-right"><span>距离'+distance+'</span></div>';
										htmlStr += '</div>';
										htmlStr += '<div class="ldh-storeList-infoM">';
										htmlStr += '<div class="fl-left"><span>起送金额(元)：<b class="money">'+minAmount+'</b></span><span>配送范围：<b class="free">'+range+'</b></span><span>营业时间：<b class="time">'+businessHours+'</b></span></div>';
										htmlStr += '<div class="fl-right"><span class="ldh-store-star">';
										htmlStr += '<em style="width:'+score+'%"></em>';
										htmlStr += '</span></div>';
										htmlStr += '</div>';
										htmlStr += '<div class="ldh-storeList-infoB">';
										htmlStr += '<div class="fl-left"><span>店铺地址：'+data["shopList"][i]["address"]+'</span></div>';
										if(businessStatus=="1"){
											htmlStr += '<div class="fl-right"><span class="ldh-store-state">营业中</span></div>';
										}else{
											
											htmlStr += '<div class="fl-right"><span class="ldh-store-state off">休息中</span></div>';
										}
										
										htmlStr += '</div>';
										htmlStr += '</div>';
										htmlStr += '</li>';
										
										
							}
							
							if(htmlStr==""){
								
								htmlStr='<li>找不到符合条件的商家</li>';
							}
							
							$("#shopList").html(htmlStr);
							
							//data["pageSize"],data["pageNumber"],data["totalCount"],data["totalPage"]
							 $('#footPage').ldh_pagination_shoplist(data["pageSize"],data["pageNumber"],data["totalCount"],data["totalPage"],{  
								 
								 changePageCallback:function(pageSize,page){  
									
									 exports.setCurrentPage(page);
									 exports.getShopList();
								 },
								 changePageSizeCallBack:function(pageSize){
								 } 
					         }); 

						}
						
						base.cancelLoading("#shopList");
						
						
					}))().start();
			
	};
	
});

